<?php

Yii::import( "application.controllers.FrontendBaseController" );

class PhotoController extends FrontendBaseController
{
    public function actionIndex( $id )
    {                
        $album = PhotoFacade::getAlbumByPhoto($id);
        if ($album === null)
        {
            throw new CHttpException( 404, 'Фотография не найдена' );
        }
        
        $user = PhotoFacade::getUserByAlbum($album->album_id);        
        
        if ($user->user_id == $this->getUserIdFromSession())
        {
            $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Фотоальбомы" => '/albums/' . $this->getUserIdFromSession(),
                                    $album->name    => '/album/' . $album->album_id,
                                    'Просмотр фотографии'    => '');
        }        
        else
        {
            $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    $user->surname . " " . $user->name => '/user/' . $user->user_id,
                                    "Фотоальбомы" => '/albums/' . $user->user_id,
                                    $album->name    => '/album/' . $album->album_id,
                                    'Просмотр фотографии'    => '');
        }
        
        $actionRights = $this->isAuthor($user);
        
        $photo = PhotoFacade::getPhotoById($id);
        
        $this->render("photo", array(
            'photo'     => $photo,
            'author'    => $user,
            'album'     => $album,
            'rights'    => $actionRights
        ));
        
    }
    
    public function actionAdd()
    {
        $user = $this->getUserModel();  
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Создание фотоальбома" => "" );
        
        if (!isset($_POST['PhotoAlbum']))
        {
            $model = new PhotoAlbum();
        
            $this->render('add', array('model' => $model));
        }
        else
        {
            $album = new PhotoAlbum();
            $album->attributes = $_POST['PhotoAlbum'];
            $album->user_id = $user->user_id;            
            $album->save();
            
            $this->render('add', array(
                'success'   => true,
                'album'     => $album
                )
            );
        }
        
    }
    
    public function actionDelete( $id )
    {
        $user = $this->getUserModel();
        
        $photo = PhotoFacade::getPhotoById($id);
        if ($photo === null)
        {
            throw new CHttpException( 404, 'Фотография не найдена' );
        }
        
        $photoOwner = PhotoFacade::getUserByAlbum($photo->album_id);
        
        if ($user->user_id != $photoOwner->user_id)
        {
            throw new CHttpException( 500, 'Нет доступа' );
        }
        
        PhotoFacade::deletePhoto($photo);
        
        $this->redirect("/album/" . $photo->album_id);
                
    }
    
    public function actionAll()
    {
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Все фотографии" => "" );
        
        $photos = PhotoFacade::getAllPhotos();
        
        $this->render("all", array("photos" => $photos));
    }
    
    private function getUserModel($id = null)
    {
        if (!is_null($id))
        {
            $user = UserFacade::getUserById($id);
        }
        else
        {
            $user = UserFacade::getUserById($this->getUserIdFromSession());
        }
        if ($user === null)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        return $user;
    }
    
}
?>
